package cz.muni.fi.pa165.pokemon.service.impl;

import cz.muni.fi.pa165.pokemon.dao.BadgeDAO;
import cz.muni.fi.pa165.pokemon.dto.BadgeTO;
import cz.muni.fi.pa165.pokemon.entity.Badge;
import cz.muni.fi.pa165.pokemon.service.BadgeService;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Dusan Svancara
 */
@Service
public class BadgeServiceImpl implements BadgeService {
    
    @Autowired
    private BadgeDAO dao;
    
    @Autowired
    private DozerBeanMapper mapper;

    @Transactional
    @Override
    public void create(BadgeTO badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() != null) {
            throw new IllegalArgumentException("badge.id is not null");
        }
        
        Badge c = mapper.map(badge, Badge.class);
        dao.create(c);
        badge.setId(c.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public BadgeTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        Badge badge = dao.get(id);
        if (badge == null) {
            return null;
        }
        return mapper.map(badge, BadgeTO.class);
    }

    @Transactional
    @Override
    public void update(BadgeTO badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() == null) {
            throw new IllegalArgumentException("badge.id is null");
        }

        Badge c = mapper.map(badge, Badge.class);
        dao.update(c);
    }

    @Transactional
    @Override
    public void remove(BadgeTO badge) {
        if (badge == null) {
            throw new IllegalArgumentException("badge is null");
        }
        if (badge.getId() == null) {
            throw new IllegalArgumentException("badge.id is null");
        }
        Badge c = mapper.map(badge, Badge.class);
        dao.remove(c);
    }

    @Transactional(readOnly = true)
    @Override
    public List<BadgeTO> findAll() {
        List<Badge> res = dao.findAll();
        List<BadgeTO> retVal = new ArrayList<>();
        for (Badge c : res) {
            retVal.add(mapper.map(c, BadgeTO.class));
        }
        
        return retVal;
    }

    @Transactional(readOnly = true)
    @Override
    public List<BadgeTO> findByName(String firstName, String lastName) {
        boolean firstNameNull = (firstName == null);
        boolean lastNameNull = (lastName == null);
        if (lastNameNull && firstNameNull) {
            throw new IllegalArgumentException("null lastName and firstName");
        }

        List<Badge> res = dao.findByName(firstName, lastName);
        List<BadgeTO> retVal = new ArrayList<>();
        for (Badge c : res) {
            retVal.add(mapper.map(c, BadgeTO.class));
        }
        
        return retVal;
    }
    
}
